package com.rbcs.base.item.webapp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.rbcs.base.item.service.ItemManager;
import com.rbcs.base.model.Item;
import com.rbcs.base.model.Project;
import com.systop.common.modules.dept.model.Dept;
import com.systop.core.Constants;
import com.systop.core.webapp.struts2.action.DefaultCrudAction;

@SuppressWarnings("serial")
@Controller
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class ItemAction extends DefaultCrudAction<Item, ItemManager> {

	/** 上级科目ID */
	private Integer parentId;

	/** AJAX调用返回变量(JSON) */
	private List<Map<String, Object>> itemMaps;

	/** 财务科目是否被选中 */
	private String checked;

	private List<Item> topItems;

	/**
	 * 科目treeTable's list
	 */
	public String index() {
		topItems = getManager().getByParentId(null);
		Item parnet = getModel().getParent();
		if (parnet != null && parnet.getId() != null){
			parnet = getManager().get(parnet.getId());
			getModel().setParent(parnet);
			items = getManager().getTreeItems(parnet.getId());
		}else{
			getModel().setParent(null);
			items = getManager().getTreeItems(null);
		}
		return INDEX;
	}

	/**
	 * 保存预算科目
	 */
	public String save() {
		if (parentId != null && parentId != 0) {
			Item parent = getManager().get(parentId);
			parent.setIsLeaf(Constants.NO);
			getModel().setParent(parent);
		}
		return super.save();
	}

	/**
	 * 构建科目Tree
	 * 
	 * @return
	 */
	public String itemTree() {
		List<Item> tops = getManager().getByParentId(null);
		itemMaps = new ArrayList<Map<String, Object>>();
		for (Item item : tops) {
			Map<String, Object> top = toMap(item);
			top = buildTreeByParent(top, true);
			itemMaps.add(top);
		}
		return "tree";
	}

	/**
	 * 根据父ID构建JSON数据
	 * 
	 * @param parent
	 * @param nested
	 *            是否嵌套
	 * @return
	 */
	private Map<String, Object> buildTreeByParent(Map<String, Object> parent,
			boolean nested) {
		if (parent == null || parent.get("id") == null) {
			return null;
		}
		List<Item> subs = getManager()
				.getByParentId((Integer) parent.get("id"));
		List<Map<String, Object>> children = new ArrayList<Map<String, Object>>();
		for (Item sub : subs) {
			Map<String, Object> child = toMap(sub);
			if (nested) {
				child = buildTreeByParent(child, nested);
			}
			children.add(child);
		}
		if (!children.isEmpty()) {
			parent.put("children", children);
			parent.put("leaf", false);
		} else {
			parent.put("leaf", true);
		}
		return parent;
	}

	private Map<String, Object> toMap(Item i) {
		Map<String, Object> map = null;
		if (i != null) {
			map = new HashMap<String, Object>();
			map.put("id", i.getId());
			map.put("text", i.getName());
		}
		return map;
	}
	
	private List<Item> parents;

	private Dept dept;

	private Project project;

	private String inputType;

	/**
	 * 财务科目选择
	 * 
	 * @return
	 */
	public String selectItem() {
		if (dept != null && dept.getId() != null) {
			dept = getManager().getDao().get(Dept.class, dept.getId());
			items = dept.getItems();
		} else if (project != null && project.getId() != null) {
			project = getManager().getDao().get(Project.class, project.getId());
			items = project.getProjectType().getItems();//获得项目类别关联的预算科目
		} else {
			parents = getManager().getByParentId(null);
			if (StringUtils.isNotBlank(getModel().getName())) {// 根据名称查询
				items = getManager().getSubByName(getModel().getName());
			} else if (parentId != null) {// 根据上级科目查询
				items = getManager().getByParentId(parentId);
			} else {// 获得全部二级科目
				items = getManager().getSubs();
			}
		}
		return "select";
	}

	public Dept getDept() {
		return dept;
	}

	public void setDept(Dept dept) {
		this.dept = dept;
	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	public String isInputType() {
		return inputType;
	}

	public void setInputType(String inputType) {
		this.inputType = inputType;
	}

	public List<Map<String, Object>> getItemMaps() {
		return itemMaps;
	}

	public Integer getParentId() {
		return parentId;
	}

	public void setParentId(Integer parentId) {
		this.parentId = parentId;
	}

	public List<Item> getParents() {
		return parents;
	}

	public String getChecked() {
		return checked;
	}

	public void setChecked(String checked) {
		this.checked = checked;
	}

	public List<Item> getTopItems() {
		return topItems;
	}
	
}
